var timers = new Array();
var refresher;
var tpos = 0;

String.prototype.startsWith = function(str) 
{return (this.match("^"+str)==str);};

function setVisible(obj)
{
	var iwidth = 0;
	var iheight = 0;
	if(self.innerWidth!=undefined){
		iwidth= self.innerWidth;
		iheight = self.innerHeight;
	}
	else{
		var D= document.documentElement;
		if(D){
			iwidth = D.clientWidth;
			iheight = D.clientHeight;
		}
	}
	obj = document.getElementById(obj);
	var vwidth = parseInt(obj.style.width.replace(/[^0-9]/g, ''));
	var vheight = obj.style.height.replace(/[^0-9]/g, '');
	var newleft = ((iwidth/2)-(vwidth/2)) + "px";
	var newtop = ((iheight/2)-(vheight/2)) + "px";
	obj.style.left = "" + newleft;
	obj.style.top = newtop;
	obj.style.visibility = (obj.style.visibility == 'visible') ? 'hidden' : 'visible';
}

function changeContent(obj){
	clearTimers();
	clearTimeout(refresher);
	var url = obj;
	
	if (window.ActiveXobject)
	{// code for IE6, IE5
		xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	}else if (window.XMLHttpRequest)
	{// code for IE7+, , Chrome, Opera, Safari
		xmlhttp=new XMLHttpRequest();
	}
	xmlhttp.onreadystatechange=function()
	{
		if (xmlhttp.readyState==0 || xmlhttp.readyState==1 || xmlhttp.readyState==2 || xmlhttp.readyState==3 || xmlhttp.readyState==4)
		{
			document.getElementById("content").innerHTML="Loading...";
		}
		if (xmlhttp.readyState==4 && xmlhttp.status==200)
		{
			document.getElementById("content").innerHTML=xmlhttp.responseText;
			if(url == "auctions.jsp"){
				refresher = setTimeout('changeContent("auctions.jsp")',10000);
			}
			else if(url == "deleteauctions.jsp"){
				refresher = setTimeout('changeContent("deleteauctions.jsp")',10000);
			}
			else if(url.substring(0, 15) == "viewAuction.jsp"){
				refresher = setTimeout('updateBids("bids.jsp?id=' + url.substr(19) + '")',10000);
			}
			var x = document.getElementById("content").getElementsByTagName("script");
			for(var i=0;i<x.length;i++){
				eval(x[i].text);
			}
		}
	};
	xmlhttp.open("GET",url,true);
	xmlhttp.send();
}

function updateBids(obj){
	clearTimeout(refresher);
	var url = obj;
	
	if (window.ActiveXobject)
	{// code for IE6, IE5
		xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	}else if (window.XMLHttpRequest)
	{// code for IE7+, , Chrome, Opera, Safari
		xmlhttp=new XMLHttpRequest();
	}
	xmlhttp.onreadystatechange=function()
	{
		if (xmlhttp.readyState==0 || xmlhttp.readyState==1 || xmlhttp.readyState==2 || xmlhttp.readyState==3 || xmlhttp.readyState==4)
		{
			document.getElementById("prizes").innerHTML="Loading...";
		}
		if (xmlhttp.readyState==4 && xmlhttp.status==200)
		{
			document.getElementById("prizes").innerHTML=xmlhttp.responseText;
			refresher = setTimeout('updateBids("bids.jsp?id=' + url.substr(12) + '")',10000);
		}
	};
	xmlhttp.open("GET",url,true);
	xmlhttp.send();
}

function changeCont(target, obj){
	document.getElementById(target).innerHTML=obj;
}

function initTimer(id, dend){
	timers[tpos] = setInterval("update(\"" + id + "\",\"" + dend + "\")",1000);
	tpos++;
	update(id,dend);
}

function clearTimers(){
	for(var i = 0; i < tpos; i++){
		clearInterval(timers[i]);
		timers[i] = 0;
	}
}

function update(target, dend){
	var a=dend.split(" ");
	d=a[0].split("-");
	t=a[1].split(":");
	var end = new Date(d[0],(d[1]-1),d[2],t[0],t[1],t[2]);
	now = new Date();
	time = (end.getTime() - now.getTime());
	var x = 0;
	if(time > 0 && time < 1000){
		x=time;
	}else if(time < 0){
		x = -1;
	}
	time = time/1000;
 	tmpday = Math.floor(time/(24*60*60));
	tmphour = Math.floor((time%(24*60*60))/(60*60));
	tmpmin = Math.floor(((time%(24*60*60))%(60*60))/60);
	tmpsec = Math.floor(((time%(24*60*60))%(60*60))%60);
	if(tmphour < 10) tmphour = "0" + tmphour;
	if(tmpmin < 10) tmpmin = "0" + tmpmin;
	if(tmpsec < 10) tmpsec = "0" + tmpsec;
	var output = "";
	if(tmpday > 0) output = tmpday + "d ";
	if(tmpday > 0 || tmphour > 0) output = output + tmphour + "h ";
	if(tmpday > 0 || tmphour > 0 || tmpmin > 0) output = output + tmpmin + "m ";
	if(tmpday > 0 || tmphour > 0 || tmpmin > 0 || tmpsec > 0) output = output + tmpsec + "s";
	if(x > 0){
		document.getElementById(target).innerHTML = "00s";
	}else if(x == -1){
		document.getElementById(target).innerHTML = "CLOSED";
	}else if(document.getElementById(target)!=null){
		document.getElementById(target).innerHTML = output;
	}
}

function highest(one, two){
	if(parseInt(one) > parseInt(two)){return one;}else{return two;}
}

function getTime(){
	return new Date().getTime();
}

function changeBidForm(auctionID, auctionName){
	document.getElementById("auctionName").innerHTML = auctionName;
	document.getElementById("auctionIDinput").value = auctionID;
}

function refresh(){
	var xmlHttp;
	try{	
		xmlHttp=new XMLHttpRequest();// Firefox, Opera 8.0+, Safari
	}
	catch (e){
		try{
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP"); // Internet Explorer
		}
		catch (e){
		   try{
				xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch (e){
				alert("No AJAX!?");
				return false;
			}
		}
	}

	xmlHttp.onreadystatechange=function(){
		if(xmlHttp.readyState==4){
			clearTimers();
			document.getElementById('content').innerHTML=xmlHttp.responseText;
			setTimeout('refresh()',10000);
			var x = document.getElementById("content").getElementsByTagName("script");
			for(var i=0;i<x.length;i++){
				eval(x[i].text);
			}
		}
	};
	
	xmlHttp.open("GET","auctions.jsp",true);
	xmlHttp.send(null);
}